package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import io.swagger.annotations.Api;
import org.apache.commons.io.FilenameUtils;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("/admin/product")
@RestController
@Api(tags = "图片上传")
public class FileUploadController {

    @Value("${imageUrl}")
    private String imageUrl;

    @PostMapping("/fileUpload")
    public Result<String> fileUpload(MultipartFile file) throws Exception{
        //0
        String path = ClassUtils.getDefaultClassLoader().getResource("file_client.conf").getPath();
        ClientGlobal.init(path);
        //1
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();

        //2
        StorageClient1 storageClient1 = new StorageClient1(trackerServer,null);

        //3
        String Ext = FilenameUtils.getExtension(file.getOriginalFilename());
        String file1 = storageClient1.upload_file1(file.getBytes(), Ext, null);
        System.out.println(imageUrl+file1);
        return Result.ok(imageUrl+file1);
    }
}
